package com.sairui.Xiaoruilib;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.view.Window;

import com.sairui.Xiaoruilib.bean.CardOptionEntity;
import com.sairui.Xiaoruilib.bean.MoreAskEntity;
import com.sairui.Xiaoruilib.manager.SimpleTitle;
import com.sairui.Xiaoruilib.widgets.flowlayout.FlowLayout;
import com.sairui.Xiaoruilib.widgets.flowlayout.TagAdapter;
import com.sairui.Xiaoruilib.widgets.flowlayout.TagFlowLayout;

import java.lang.ref.WeakReference;
import java.util.Collections;


public class CardOptionActivity extends Activity {
    TextView optionTitle;
    TagFlowLayout hotSearch;
    private TagAdapter mHotAdapter;
    private MoreAskEntity moreAskEntity;

    public static Intent getInstance(Context context, MoreAskEntity moreAskEntity) {
        Intent intent = new Intent(context, CardOptionActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", moreAskEntity);
        intent.putExtras(bundle);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(MResource.getIdByName(this, "layout", "activity_card_option"));
        SimpleTitle mTitle = new SimpleTitle(new WeakReference<Activity>(this));
        mTitle.setBack();

        optionTitle = findViewById(MResource.getIdByName(this, "id", "option_title"));
        hotSearch = findViewById(MResource.getIdByName(this, "id", "hot_search"));

        moreAskEntity = (MoreAskEntity) getIntent().getSerializableExtra("data");
        optionTitle.setText(moreAskEntity.getCardTitle());

        Collections.sort(moreAskEntity.gettCardOptionList());
        mHotAdapter = new TagAdapter<CardOptionEntity>(moreAskEntity.gettCardOptionList()) {
            @Override
            public View getView(FlowLayout parent, int position, final CardOptionEntity entity) {
                View view = LayoutInflater.from(CardOptionActivity.this).inflate(MResource.getIdByName(CardOptionActivity.this,"layout","adapter_search_content_item"), parent, false);
                TextView tvItemSpec = (TextView) view.findViewById(MResource.getIdByName(CardOptionActivity.this,"id","item"));
                tvItemSpec.setText("DIGITAL_SERIAL".equals(moreAskEntity.getOptionCardSerialStyleCode()) ?
                        (position + 1) + "." + entity.getOptionItemName() : entity.getOptionItemName());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("key", entity.getOptionItemName());
                        setResult(9527, intent);
                        finish();
                    }
                });
                return view;
            }
        };
        hotSearch.setAdapter(mHotAdapter);
    }

}
